# coding=utf-8
# 全世界最帅的帅哥
from flask import request, Response
from flask_restful import Resource
from ysgg1_club_wx.utils.WXMsg import parse_xml, handle_msg
from ysgg1_club_wx.wx_views.wx_view import callback


class WxResource(Resource):
    def get(self):
        echostr = callback()
        return echostr

    def post(self):
        '''
            ImmutableMultiDict([
            ('signature', '28311b29acf940396ac145f7a1da26905d031da8'),
            ('timestamp', '1737819455'),
            ('nonce', '655469816'),
            ('openid', 'oK69H7Bc6dvFOLn9UbhQwNEhXLaM')])
        '''

        """ 
            <xml>
                <ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName>
                <FromUserName><![CDATA[gh_7f083739789a]]></FromUserName>
                <CreateTime>1407743423</CreateTime>
                <MsgType>  <![CDATA[video]]></MsgType>
                <Video>
                    <MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId>
                    <Title><![CDATA[testCallBackReplyVideo]]></Title>
                    <Descript  ion><![CDATA[testCallBackReplyVideo]]></Description>
                </Video>
            </xml>
        """
        # 关注的xml
        '''
            <xml>
                <ToUserName><![CDATA[gh_a244ea59be20]]></ToUserName>
                <FromUserName><![CDATA[oK69H7Bc6dvFOLn9UbhQwNEhXLaM]]></FromUserName>
                <CreateTime>1737819269</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[subscribe]]></Event>
                <EventKey><![CDATA[]]></EventKey>
            </xml>
        '''

        '''
            <xml>
                <ToUserName><![CDATA[gh_1a89ff67b7b9]]></ToUserName>
                <Encrypt><![CDATA[QNmBIk5Afjaaa2xBpZXoHs/2srUmDwk7R+c9GRBrORJ82eFdvtZ0S0qg/0q/h41kyq+fV3gEAXWG9UXAWDY
        T6SgA2KTlvGTWJstRYvPqEX05IJFb6tiXm5By4Uqjzfppet62 / 0
        QH7udfLD8jmtzOsa05WGq5H5phMmUCRjJXTQ / Q9oTu7h2mncBFChv / iK7OD09eo1NPJeld6gR2UiQrzukg7 + lNwpzmFhrnuvEk9Zf96kA1vrD2brLowuudKzM
        sPS5lsu17kyiplvfpxzHzKmYCgUZsgenvARNXILCP2oguq2FgLxAFchQpPJR6SnKKHYRjS64xJIpS3FlE0s55U6K913Zdah / AWYxl + xKICY1PaEMejyLG / 9U
        9
        vs6Zgw3JBoxTtFsJdy0QACZBsxjkXxoFAwMBStTDbSeba4r / dws =]] > < / Encrypt >
        \n < / xml >\n
        '''

        # 发送普通消息的xml
        '''
            <xml>
                <ToUserName><![CDATA[gh_a244ea59be20]]></ToUserName>
                <FromUserName><![CDATA[oK69H7Bc6dvFOLn9UbhQwNEhXLaM]]></FromUserName>
                <CreateTime>1737819454</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[验证码]]></Content>
                <MsgId>24879593478818925</MsgId>
            </xml>
        '''

        '''
            <xml>
                <ToUserName><![CDATA[gh_1a89ff67b7b9]]></ToUserName>
                <Encrypt><![CDATA[3q781ec6gtZBL8TErJgswte/hXe2d7xOq8F27cB9QlNKeZidDGcw9GYpjakyiN4lXkY8DUo9hVeaU4Ct/9w0qeodYHxilbfX +
                nlmGSIHygZSCPnDsqNrNtx7dVPwCQy1W7DV3wmgNRbDz378gi4LSAMNEmNwGr / PSvFZ8 +
                U0LIMKaqU88YIvf1ZMk8xKKLe3lC4uxO8D3F0hHs0LTKrDCTRaeXzCUEFGoIEqnB8QmBnmMdp / GWAjVu0xfO70V6hn1hzM1NSLVJ2 +
                jWXSiX22TniRjC0lpgylGQtpMYL2QLpel5NP6nl1w1ZIWF5OPjDKez01SGzASCvu8fwce4df0SwbraSpWWE9ZsJrDJS2wcEYsuXw487u +
                CBKETAD6IubcrbS7iJvi8IQ20TUDBMZD9wG / hdG8fWSoFX8PQ =]]>
                </Encrypt>
            </xml>
        '''

        xml_text = request.data
        xml_dict = parse_xml(xml_text)
        xml_data = handle_msg(xml_dict)
        print(xml_data)
        response = Response(xml_data, content_type='application/xml; charset=utf-8')
        return response


